ManifestDPIAware true

!define APP_NAME "<%= meta.long_name %>"
!define COMP_NAME "<%= package.windows.publisher %>"
!define VERSION "<%= meta.version %>.0"
!define COPYRIGHT "<%= package.windows.publisher %>"
!define DESCRIPTION "<%= meta.description %>"
!define INSTALLER_NAME "installer.exe"
!define MAIN_APP_EXE "<%= package.binary_name %>.exe"
!define ICON "icon.ico"
!define LICENSE_TXT "LICENSE.txt"
<% if (target.arch == "x86_64") { %>
!define INSTALL_DIR "$PROGRAMFILES64\${APP_NAME}"
<% } else { %>
!define INSTALL_DIR "$PROGRAMFILES\${APP_NAME}"
<% } %>
!define INSTALL_TYPE "SetShellVarContext all"
!define REG_ROOT "HKLM"
!define REG_APP_PATH "Software\Microsoft\Windows\CurrentVersion\App Paths\${MAIN_APP_EXE}"
!define UNINSTALL_PATH "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APP_NAME}"

######################################################################

VIProductVersion  "${VERSION}"
VIAddVersionKey "ProductName"  "${APP_NAME}"
VIAddVersionKey "CompanyName"  "${COMP_NAME}"
VIAddVersionKey "LegalCopyright"  "${COPYRIGHT}"
VIAddVersionKey "FileDescription"  "${DESCRIPTION}"
VIAddVersionKey "FileVersion"  "${VERSION}"

######################################################################

SetCompressor /SOLID Lzma
Name "${APP_NAME}"
Caption "${APP_NAME}"
OutFile "${INSTALLER_NAME}"
BrandingText "${APP_NAME}"
InstallDirRegKey "${REG_ROOT}" "${REG_APP_PATH}" ""
InstallDir "${INSTALL_DIR}"

######################################################################

!define MUI_ICON "${ICON}"
!define MUI_UNICON "${ICON}"

######################################################################

!include "MUI2.nsh"

!define MUI_ABORTWARNING
!define MUI_UNABORTWARNING

!insertmacro MUI_PAGE_WELCOME

!ifdef LICENSE_TXT
!insertmacro MUI_PAGE_LICENSE "${LICENSE_TXT}"
!endif

!insertmacro MUI_PAGE_DIRECTORY

!insertmacro MUI_PAGE_INSTFILES

!insertmacro MUI_PAGE_FINISH

!insertmacro MUI_UNPAGE_CONFIRM

!insertmacro MUI_UNPAGE_INSTFILES

!insertmacro MUI_UNPAGE_FINISH

!insertmacro MUI_LANGUAGE "English"

######################################################################

Section -MainProgram
	${INSTALL_TYPE}

  SetDetailsPrint textonly
  DetailPrint "Installing..."
  SetDetailsPrint listonly

	SetOverwrite ifnewer
	SetOutPath "$INSTDIR"
	File /r "install_dir\\"

SectionEnd

######################################################################

Section -Icons_Reg
SetOutPath "$INSTDIR"
WriteUninstaller "$INSTDIR\uninstall.exe"

WriteRegStr ${REG_ROOT} "${REG_APP_PATH}" "" "$INSTDIR\${MAIN_APP_EXE}"
WriteRegStr ${REG_ROOT} "${UNINSTALL_PATH}"  "DisplayName" "${APP_NAME}"
WriteRegStr ${REG_ROOT} "${UNINSTALL_PATH}"  "UninstallString" "$INSTDIR\uninstall.exe"
WriteRegStr ${REG_ROOT} "${UNINSTALL_PATH}"  "DisplayIcon" "$INSTDIR\${MAIN_APP_EXE}"
WriteRegStr ${REG_ROOT} "${UNINSTALL_PATH}"  "DisplayVersion" "${VERSION}"
WriteRegStr ${REG_ROOT} "${UNINSTALL_PATH}"  "Publisher" "${COMP_NAME}"

<% for (let name in store) { %>
<%   let regs = store[name].msg_manifest_paths.windows.regs; %>
<%   for (let reg of regs) { %>
WriteRegStr ${REG_ROOT} "<%= reg %>\<%= meta.id %>" "" "$INSTDIR\<%= name %>.json"
<%   } %>
<% } %>

SectionEnd

######################################################################

Section Uninstall
${INSTALL_TYPE}

RmDir /r "$INSTDIR"

DeleteRegKey ${REG_ROOT} "${REG_APP_PATH}"
DeleteRegKey ${REG_ROOT} "${UNINSTALL_PATH}"

<% for (let name in store) { %>
<%   let regs = store[name].msg_manifest_paths.windows.regs; %>
<%   for (let reg of regs) { %>
DeleteRegKey ${REG_ROOT} "<%= reg %>\<%= meta.id %>"
<%   } %>
<% } %>


SectionEnd

